<template>
	<view class="center-view">

		<!-- <view v-for="(question,index) in questions" :key="index">
			<ztext v-if="question.type=='ztext'" :que="question.question" :restrict="question.answer.restrict" :length="question.answer.length" :empty="question.answer.empty" :ref="question.key" />
			<zradio v-if="question.type=='zradio'" :que="question.question" :choices="question.answer.choices" :empty="question.answer.empty" :ref="question.key" />
			<zcheckbox v-if="question.type=='zcheckbox'" :que="question.question" :choices="question.answer.choices" :minNum="question.answer.minNum" :maxNum="question.answer.maxNum" :ref="question.key" />
			<zpicker v-if="question.type=='zpicker'" :que="question.question" :choices="question.answer.choices" :empty="question.answer.empty" :ref="question.key" />
			
		</view> -->
		<view class="info-item dashboard" ><text class="info-title">发起时间</text>
		<text class="info-status">{{formatDatetime(new Date(startTime))}}</text></view>
		<view class="info-item dashboard" ><text class="info-title">截止时间</text>
		<text class="info-status">{{planEndTime.length>0 ? planEndTime : '无限制'}}</text></view>
		<view class="info-item dashboard"><text class="info-title">状态</text><text class="info-status">{{getStatusText(status)}}</text></view>
		<view class="info-item dashboard"><text class="info-title">报名主题</text><text class="info-status">{{title}}</text></view>
		<view class="info-item dashboard"><text class="info-title">发起者</text><text class="info-status">{{host}}</text></view>
		<zEnrollItemPanel v-if="status==0" :noUpload="false" :questions="questions" :ref="'answerPanel'" style="width:98%;margin-left:0%;"/>
		<button  @click="submit" v-if="status==0" type="primary" class="long-green-button">提交</button>
	</view>
</template>


<script>
	import formatDatetime from "../../common/zdateutil.js"
	import zEnrollItemPanel from "../../components/basic/zenrollitempanel";
	export default {
		data() {
			return {
				eId : "",
				questions : [],
				planEndTime : "",
				startTime : "",
				status : -1,
				title : "",
				formatDatetime : null,
				host : ""
				// keys : []
			}
		},
		components : {
			zEnrollItemPanel
		},
		onLoad(option) {
			// this.$refs.q1.question = "大象的鼻子有多长？"
			// console.log(option.eId)
			var param = {'eId':option.eId};
			this.eId = option.eId;
			
			var vm = this;

			// 首先查看是否已经提交过报名
			this.$get(this.enrollurl + '/enroll/enrollResult',param,function(res){
				// console.log(res)
				vm.formatDatetime = formatDatetime.formatDatetime;
				var data = res.data;
				if(data.code != 0){
					uni.showModal({
						title : '错误',
						content : data.message,
						showCancel : false
					})
					return ;
				}
				if(data.object.status != -1){
					uni.navigateTo({
						url : './enrollResult?eId=' + param.eId + '&statusTip=' + data.object.statusTip + '&status=' 
						+ data.object.status + '&content=' + data.object.content + '&submitTime=' + data.object.submit_time
					})
				}
			})
			// // var statusTip = "";
			this.$get(this.enrollurl + '/enroll/getEnrollMeta',param,function(res){
				var data = res.data;
				if(data.code != 0){
					uni.showModal({
						title : '错误',
						content : data.message,
						showCancel : false
					})
					return ;
				}
				data = data.object;
				console.log(data.object)
				vm.questions = JSON.parse(data.content);
				if((data.endMethod >> 1 & 1) == 1){
					vm.planEndTime = formatDatetime.formatDatetime(new Date(data.planEndTime));
					// console.log(vm.planEndTime)
				}
				vm.status = data.status;
				vm.startTime = data.startTime;
				vm.title = data.title;
				vm.host = data.host;
				// vm.statusTip = data.statusTip
				// console.log(data.content)
			});
		},
		methods: {
			getStatusText : function(status){
				if(status == 0) return '进行中';
				else return '已终止';
			},
			submit : function(){
				// console.log(this.keys)
				// window.test = this.$refs
				// console.log(this.$refs)
				// .answerPanel.get()
				// console.log(this.$refs['answerPanel'])
				var content = this.$refs['answerPanel'].getAns();
				// console.log(content);
				// return ;
				if(content == null)
					return ;
				var vm = this;
				var param = {'eId':this.eId,'content':JSON.stringify(content)}
				this.$post(this.enrollurl + '/enroll/submitEnroll',param,function(res){
					var data = res.data;
					if(data.code != 0){
						uni.showModal({
							title : '错误',
							content : data.message,
							showCancel : false
						})
						return ;
					}
					uni.navigateTo({
						url : 'enrollResult?eId=' + vm.eId
					})
				});
				// console.log(this.$refs.q1.get())
				// console.log(this.$refs.q2.get())
				// console.log(this.$refs.q3.get())
				// console.log(this.$refs.q4.get())
			}
		}
	}
</script>

<style>
	/* .input{
		width:200upx;
	} */
	@import "../../common/zcss.css";
	/* .enroll-view{
		flex-direction: column;
	} */
	.info-item{
		border: 1upx solid gray;
		height: 80upx;
		position: relative;
		width: 99%;
		margin-bottom: 3upx;
		/* background-color: white; */
		/* background-color: azure; */
	}
	.info-status{
		position: absolute;
		font-size: 40upx;
		right: 1%;
		top: 1%;
	}
	.info-title{
		font-size: 40upx;
		position: absolute;
		top: 1%;
		left: 1%;
		color:gray;
	}
</style>
